package id.khairizalnetani.tugasakhir;

import id.khairizalnetani.tugasakhir.entity.Siswa;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SelamatDatangActivity extends RoboActivity implements OnClickListener
{
	@InjectView(R.id.btnIsiLaporanKekuatan)
	Button					btnIsiLaporanKekuatan;
	@InjectView(R.id.btnKeluarSelamatDatang)
	Button					btnKeluar;
	@InjectView(R.id.btnLihatDataLaporan)
	Button					btnLihatLaporan;

	@InjectView(R.id.txtSiswa)
	TextView				txtSiswa;

	private Siswa			siswa;

	private ProgressDialog	pDialog;
	private int				idSiswa;
	private DBAdapter		db;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selamat_datang);
		btnIsiLaporanKekuatan.setOnClickListener(this);
		btnKeluar.setOnClickListener(this);
		btnLihatLaporan.setOnClickListener(this);

		db = new DBAdapter(this);
		db.open();

		Bundle bundle = getIntent().getExtras();
		if (bundle != null)
		{
			idSiswa = bundle.getInt(Halaman1.TAG_ID);

		}

		new AsyncTaskWelcome().execute();

	}

	private void setTampilan()
	{
		txtSiswa.setText(siswa.getNama());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.selamat_datang, menu);
		return true;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btnIsiLaporanKekuatan:
				Bundle b = new Bundle();
				b.putInt(Halaman1.TAG_ID, siswa.getId());

				Intent i = new Intent(this, Halaman2.class);
				i.putExtras(b);
				startActivity(i);
				db.close();
				finish();

				break;

			case R.id.btnLihatDataLaporan:
				Utils.TRACE("Pilih menu laporan");
				Bundle bundle = new Bundle();
				Utils.TRACE("id siswa di Selamat datang : " + siswa.getId());
				bundle.putInt(Halaman1.TAG_ID, siswa.getId());

				Intent intent = new Intent(SelamatDatangActivity.this, HalamanLaporanActivity.class);
				intent.putExtras(bundle);

				startActivity(intent);
				db.close();
				finish();
				break;

			case R.id.btnKeluarSelamatDatang:
				db.close();
				System.exit(0);
				break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			startActivity(new Intent(this, Halaman1.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	private class AsyncTaskWelcome extends AsyncTask<Void, Void, Void>
	{
		@Override
		protected Void doInBackground(Void... params)
		{
			// TODO Auto-generated method stub

			siswa = db.getSiswaById(idSiswa);

			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			// TODO Auto-generated method stub

			pDialog.dismiss();
			setTampilan();
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute()
		{
			// TODO Auto-generated method stub
			super.onPreExecute();

			pDialog = new ProgressDialog(SelamatDatangActivity.this);
			pDialog.setMessage("Loading...");
			pDialog.setCancelable(true);
			pDialog.show();
		}
	}

}
